<!doctype html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=0, width=device-width"/>
	<title>api</title>
	<link rel="stylesheet" type="text/css" href="../css/api.css" />
	<link rel="stylesheet" type="text/css" href="../css/common.css" />
	<link rel="stylesheet" type="text/css" href="../css/activity.css" />
	<link rel="stylesheet" type="text/css" href="../css/editShare.css" />
	<script>
    /*(function () {
        function o() {
            document.documentElement.style.fontSize = (document.documentElement.clientWidth > 414 ? 414 : document.documentElement.clientWidth) / 12 + "px"
        }
        var e = null;
        window.addEventListener("resize", function () {
            clearTimeout(e), e = setTimeout(o, 300)
        }, !1), o()
    })(window);*/
	</script>
	<style>
	html,body{
		    background: #F8F8F8;
	}
	.acctivify-header{
			position: fixed;
			left: 0;
			right: 0;
			top: 0;
			z-index: 5;
			height:44px;
		}
	</style>
	<script id='editShare-template' type="text/x-dot-template">
		<h2 class="goods-desc">第({{=it.qishu}})期 {{=it.title}}</h2>
            <div class="goods-pic">
                <img src="http://wap.jqmao.cn/statics/uploads/{{=it.thumb}}">
                
            </div>
	</script>
	
</head>
<body>
	<div class="evaluate">
		<header id='header' class='acctivify-header'>
			<div class='header'>
				<i id='back' onclick="back()"></i><h1>晒单编辑</h1>
			</div>
		</header>

    <form action="" method="post" class="smart-green" style="margin-top: 3em">
   <!--<h1>Contact Form
        <span>Please fill all the texts in the fields.</span>
        </h1> -->
		
        <div id='editShare-content' class="goods">
            
        </div>
        <input id="shopid" type="hidden" value="33217">
        <input id="periods" type="hidden" value="1092">
        <input id="shopSid" type="hidden" value="199">
        <input id="ti" type="text" name="title" placeholder="标题不超过30字">
        <label>


        <textarea id="message" name="message" placeholder="欢迎评价！内容不超过200字"></textarea>
        </label>

        <label>
            <!--晒单图片-->
            <div class="container">
                <div class="row" style="width: 200px;margin: auto">
                    <div style="border: 1px solid;" id="f1" onclick="addImg()"  class="file">添加晒单图片
                     
                    </div>
                </div>
            </div>
        </label>
         
        <span id="btnSubmit" onclick="save()" class="save" name="submit" title="提交晒单">提交晒单</span>
        
    </form>

    </div>
</body>
	<script type="text/javascript" src="../script/zepto.min.js"></script>
	<script type="text/javascript" src="../script/api.js"></script>
	<script type="text/javascript" src="../script/doT.min.js"></script>
	<script type="text/javascript" src="../script/common.js"></script>
	<!-- <script type="text/javascript" src="../script/main.js"></script> -->
	<script>
	
	var title = '';
	var thumb = '';
	var id = '';
	var periods = '';
	var index = 0;
	var sid = '';
	var uid = '';
	var rst = new Array;
	var hasSubmit = false;

		apiready = function(){
			headerstatus();
			uid = $api.getStorage('user').uid;
		
			title = api.pageParam.title;
			thumb = api.pageParam.thumb;
			id = api.pageParam.id;
			sid = api.pageParam.sid
			periods = api.pageParam.qishu;
			var content = $api.byId('editShare-content');
            var tpl = $api.byId('editShare-template').text;
            var tempFn = doT.template(tpl);
            $api.html(content, tempFn(api.pageParam));
		}
		function addImg(){
		 api.confirm({
            title: '图片来源',
            msg: '您想要从哪里选取图片 ?',
            buttons:['相机', '相册', '取消']
        },function(ret,err){
            var sourceType = 'album';

            if(3 == ret.buttonIndex){ // 取消
                return;
            }

            if(1 == ret.buttonIndex){ // 打开相机
                sourceType = 'camera';
            }

            api.getPicture({
                sourceType: sourceType,
                encodingType: 'jpg',
                mediaValue: 'pic',                
                destinationType: 'url',
                 targetWidth: 500,
    			targetHeight: 700,
            }, function(ret, err){
           
                if (ret) {
                
               //alert(JSON.stringify(ret))
                    var f1 = $api.byId('f1');
			        $api.before(f1, '<img src="'+ret.data+'">'); 
			       if(ret.data){
			        //alert(JSON.stringify(ret.data))
			      	 	index+=1;
                    	var i = index-1;
                    	rst[i] = ret.data;   
                    	
			       }
                    

                }
            });

        });
			
		}
		function save(){
			var message_tpl = $api.byId('message');
			var message = $api.val(message_tpl);
			var ti_tpl = $api.byId('ti');
			var ti = $api.val(ti_tpl);
			if(hasSubmit==true){
				return;
			}
			hasSubmit = true;
			
			
			
       		api.ajax({
        	url: 'http://wap.jqmao.cn/upload',
        	method: 'post',
        	data: {
           	 		values: {uid:uid,title:ti,message:message,shopId:id,periods:periods,shopSid:sid,hasSubmit:hasSubmit},
           	 		files: { 
            		"file[]":rst
        	}
       		}
       					
        	
    		}, function (ret, err) {
    		if(ret){
					if(ret.code==1){
						toast("亲，编辑一下你的中奖感言吧");
					}else if(ret.code==2||ret.code==3){
						toast("亲，其实可以稍微简洁一些的");
					}else if(ret.code==4){
						toast("亲，把你中的奖品晒出来");
					}else if(ret.code==5){
						toast("正在保存数据");
						//var winName = api.winName;
						api.sendEvent({
						    name: 'reload',
						    extra: {
						        key: 'true', 
						        
						    }
						});
						api.closeWin();
						
						
						//openWin('myShare',uid);
					}
			}else{
				//alert(JSON.stringify(err))
			}
		
		})
		}
	</script>
	
</html>